import { acceptHMRUpdate, defineStore } from 'pinia'
import { useLoginApi, useLogoutApi, clientLogoutApi, useLoginApiPhone } from '@/api/login'
import cache from '@/utils/cache'
import { Storage } from '@/utils/storage'
import { encrypt } from '@/utils/tool'
import { ElMessage } from 'element-plus'
import { i18n } from '@/i18n'

export const userStore = defineStore('userStore', {
	state: () => ({
		user: {
			id: '',
			avatar: '',
			username: '',
			departmentName: '',
			company_name: '',
			address: '',
			phone: '',
			email: '',
			website: '',
            paied_num: 0,
		},
		token: cache.getToken()
	}),
	actions: {
		setUser(val: any) {
			this.user = val
		},
		setToken(val: any) {
			this.token = val
			cache.setToken(val)
		},
		// 用户登录
		async loginAction(loginForm: any) {
			const res: any = await useLoginApi(loginForm)
			if(res.data.token){
				ElMessage.success(i18n.global.t('error.successLogin'))
				Storage.setItem('user_system',1) // 后台端
				Storage.setItem('router_key',encrypt(res.data.strator.id))
				Storage.setItem('username_key',encrypt(res.data.strator.strator_nickname))
				Storage.setItem('login_info',res.data)
				Storage.setItem('user_info',res.data.strator)
				this.setToken(res.data.token)
				// this.getUserInfoAction()
				console.log('登录成功');
			}else{
				ElMessage.error(res.msg || res.error || i18n.global.t('error.failLogin'))
				return Promise.reject(res)
			}
		},
		// 用户手机号登录
		async loginActionPhone(loginForm: any) {
			const res: any = await useLoginApiPhone(loginForm)
			console.log(res);
			if(res.data.token){
				if(res.data.user.cooperation_status == 1){
					ElMessage.success(i18n.global.t('error.successLogin'))
				}
				Storage.setItem('user_system',2) // 客户端
				Storage.setItem('router_key',encrypt(res.data.user.id))
				Storage.setItem('username_key',encrypt(res.data.user.company_name))
				Storage.setItem('login_info',res.data)
				this.setToken(res.data.token)
				// this.getUserInfoAction()
				console.log('登录成功');
			}else{
				ElMessage.error(res.msg || i18n.global.t('error.failLogin'))
				return Promise.reject(res)
			}
		},
		// 获取用户信息
		getUserInfoAction() {
			const data: any = Storage.getItem('login_info')
			const user_system: any = Storage.getItem('user_system')
			// const user_system = 2;
			// const data = {
			// 	user:{
			// 		id: 1,
			// 		name: '测试用户'
			// 	}
			// }
			if (user_system == 2) {
				this.setUser({
					id: data?.strator.id,
					avatar: data?.strator.avatar || '',
					username: data?.strator.strator_nickname,
					departmentName: '',
					company_name: data?.strator.company_name || '',
					address: data?.strator.address || '',
					phone: data?.strator.phone || '',
					email: data?.strator.email || '',
					website: data?.strator.website || '',
					paied_num: data?.strator.paied_num,
				})
			} else {
				this.setUser({
					id: data?.strator.id,
					avatar: data?.strator.avatar || '',
					username: data?.strator.strator_nickname,
					departmentName: '',
					company_name: data?.strator.company_name || '',
					address: data?.strator.address || '',
					phone: data?.strator.phone || '',
					email: data?.strator.email || '',
					website: data?.strator.website || '',
					paied_num: data?.strator.paied_num,
				})
			}
		},
		// 用户退出
		async logoutAction() {
			if(Storage.getItem('user_system') == 2){
				// await clientLogoutApi()
				// 清除客户信息
				Storage.removeItem('login_info')
				Storage.removeItem('token')
				Storage.removeItem('router_key')
				Storage.removeItem('username_key')
				Storage.removeItem('visitedViews')
				// 移除 token
				this.setToken(null)
			}else{
				// await useLogoutApi()
				// 清除用户信息
				Storage.removeItem('login_info')
				Storage.removeItem('token')
				Storage.removeItem('router_key')
				Storage.removeItem('username_key')
				Storage.removeItem('visitedViews')
				// 移除 token
				this.setToken(null)
			}
		}
	}
})
// 确保传递正确的 store 声明，本例中为 `useAuth`
if (import.meta.hot) {
    import.meta.hot.accept(acceptHMRUpdate(userStore, import.meta.hot))
}
